package de.gwtprime.server.model;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import de.gwtprime.server.sql.helper.HibernateHelper;
import de.gwtprime.shared.VoteType;

@Entity
@Table(name = "voteAnswer")
public class VoteAnswer extends AbstractVote {
	
	/**
	 * SerialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private Answer answer;
	

	public void setType(VoteType type) {
		this.type = type;
	}

	@ManyToOne
	@JoinColumn
	public Answer getAnswer() {
		return answer;
	}

	public void setAnswer(Answer answer) {
		this.answer = answer;
	}
	
	public void remove(){
		long id = getAnswer().getId();
		super.remove();
		HibernateHelper.find(Answer.class, id).decreaseVoteValueAndPersist();
	}
	
	@ManyToOne
	@JoinColumn
	public User2 getUser() {
		return user;
	}

	public void setUser(User2 user) {
		this.user = user;
	}
	
	public Long getTest() {
		if(answer == null)
			return 0l;
		return answer.getId();
	}

	public void setTest(Long test) {
		this.test = test;
	}


	private Long test = 0l;
	
}
